//
//  NProblemTableViewContentCell.m
//  nncloud
//
//  Created by gdy on 2018/9/26.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NProblemTableViewContentCell.h"

@interface NProblemTableViewContentCell()<UIScrollViewDelegate>

@property(nonatomic, strong) UILabel *contenLabel;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)UIScrollView *scrollView;

@end
#define kScrollViewHeight 110


@implementation NProblemTableViewContentCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = 0;
        [self setUI];
        self.dataArray = [NSMutableArray array];
    }
    return self;
}



- (void)setUI{
    
    self.contenLabel = [UILabel new];
    self.contenLabel.textColor = NColorFrom16RGB(cContentColor);
    self.contenLabel.numberOfLines = 0;
//    self.contenLabel.backgroundColor = [UIColor magentaColor];
    self.contenLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.contenLabel];
    [self.contenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.mas_equalTo(@5);
//        make.left.mas_equalTo(@5);
//        make.right.bottom.mas_equalTo(@0);
        //wpf
        make.left.and.top.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        
    }];
    
  
   _scrollView = [UIScrollView new];
//    _scrollView.contentSize = CGSizeMake(ScreenWidth+160, kScrollViewHeight);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = YES;
    //设置代理
    _scrollView.delegate = self;
    [self.contentView addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.contenLabel.mas_bottom).offset(0);
        make.height.equalTo(@110);
        make.width.equalTo(@(ScreenWidth));
    }];
    
}

- (void)setModel:(NProblemModel *)model
{
    _model = model;
    
    self.contenLabel.text = model.questionContent;
    YYLog(@"%lu,@%@",(unsigned long)model.attachmentList.count,model.attachmentList);
    
    [self.dataArray removeAllObjects];
    for (NSDictionary *obj in model.attachmentList) {
        NSString *imgUrl = obj[@"attachmentUrl"];
        [self.dataArray addObject:imgUrl];
        YYLog(@"%@,个数:%ld",self.dataArray,self.dataArray.count);
    }
    
    if (self.dataArray.count<4) {
        _scrollView.contentSize = CGSizeMake(ScreenWidth, kScrollViewHeight);
    }else{
        _scrollView.contentSize = CGSizeMake(ScreenWidth+160, kScrollViewHeight);
    }
//    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.contenLabel.bottom+5,ScreenWidth, kScrollViewHeight)];
    

    
    //根据图片个for循环数动态创建图片或按钮
    
    for (int i = 0;i<self.dataArray.count;i++) {
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*70+10, 10, 65, 65)];
        [imgView sd_setImageWithURL:self.dataArray[i]];
        [_scrollView addSubview:imgView];
        imgView.userInteractionEnabled = YES;
        imgView.tag = i+100;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//        [imgView addGestureRecognizer:tap];
        
        
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBigImageClick:)];
        [imgView addGestureRecognizer:tapGestureRecognizer];
    }
    
    
    //设置内容视图的滚动区域
//    _scrollView.contentSize = CGSizeMake(ScreenWidth+150, kScrollViewHeight);
//    //设置scrollView的分页属性,对contentSize进行分页切割,每页的大小为scrollView的size(非常重要)
////    _scrollView.backgroundColor = [UIColor lightGrayColor];
//    _scrollView.showsHorizontalScrollIndicator = NO;
//    _scrollView.bounces = YES;
//    //设置代理
//    _scrollView.delegate = self;
//    [self.contentView addSubview:_scrollView];

}

#pragma mark - 浏览大图点击事件
-(void)scanBigImageClick:(UITapGestureRecognizer *)tap{
    //    NSLog(@"点击图片");
    UIImageView *clickedImageView = (UIImageView *)tap.view;
    [ImageZoom imageZoomWithImageView:clickedImageView];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
